package simulation;

/**
 * Created by fengliejv on 2017/10/16.
 */
public class ImageSmoother661 {
    public int[][] imageSmoother(int[][] M) {
        int[][] result = new int[M.length][M[0].length];
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[0].length; j++) {
                int sum = 0;
                int count = 0;
                for (int k = i - 1; k <= i + 1; k++) {
                    for (int l = j - 1; l <= j + 1; l++) {
                        if(k>=0 && k<M.length && l>=0 && l<M[0].length){
                            sum += M[k][l];
                            count ++;
                        }
                    }
                }
                result[i][j] = sum/count;
            }
        }
        return result;
    }
}
